﻿using System;
using Glue;
using Glue.Internals.Verification;
using Xunit;

namespace GlueTests
{

    public class EnumRandomizerTest
    {
        private readonly EnumRandomizer randomizer;

        public enum RandomizedEnum
        {
            First,
            Second,
            Third
        }

        public EnumRandomizerTest()
        {
            randomizer = new EnumRandomizer(new Random(Environment.TickCount));
        }

        [Fact]
        public void Should_throw_meaningful_exception_when_type_is_not_enum()
        {
            Assert.Throws(typeof(GlueException), () => randomizer.Randomize(typeof(EnumRandomizerTest)));
        }

        [Fact]
        public void Should_generate_right_type()
        {
            
            Assert.Equal(typeof(RandomizedEnum),randomizer.Randomize(typeof(RandomizedEnum)).GetType());
        }

        [Fact]
        public void Should_generate_different_results()
        {
            Assert.True(AllNotEqual(() => randomizer.Randomize(typeof(RandomizedEnum)))); 
        }

        private bool AllNotEqual(Func<object> func)
        {
            var value = func();
            for (int i = 0; i < 10; i++)
            {
                var newValue = func();
                if (!value.Equals(newValue))
                    return true;
            }
            return false;
        }
    }
}
